package nl.tue.bsdddump;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;

import no.catenda.peregrine.model.objects.json.IfdConcept;
import no.catenda.peregrine.model.objects.json.IfdName;

import com.db4o.Db4oEmbedded;
import com.db4o.ObjectContainer;
import com.db4o.ObjectSet;
import com.db4o.config.EmbeddedConfiguration;
import com.db4o.query.Query;

public class DBInfo {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new DBInfo();

	}
	private static Logger logger = Logger.getLogger(PeregrineDump.class);
	private static final String DB4OFILENAME = "model.db";
	private static HashSet<String> conceptGUIDS = new HashSet<String>();
	
	private static DBSearchCatalogue catalogue ;
	static ObjectContainer model_db;
	static boolean incremental = true;
	
	public DBInfo()
	{
		
			EmbeddedConfiguration db4oConfig = Db4oEmbedded.newConfiguration();
			db4oConfig.common().objectClass(IfdConcept.class).objectField("guid").indexed(true);
			db4oConfig.common().activationDepth(10);
			db4oConfig.common().updateDepth(10);
			
			
	/*		File dbfile= new File(DB4OFILENAME);
			if (dbfile.exists())
			{
				model_db.close();
				
			}
	*/		model_db = Db4oEmbedded.openFile(db4oConfig, DB4OFILENAME);
			Query query = model_db.query();
			query.constrain(DBSearchCatalogue.class);
			int conceptcount=0;
			int namecount=0;
			
			ObjectSet catalogues = query.execute();
			if (catalogues.size() > 0){
					catalogue = (DBSearchCatalogue) catalogues.get(0);
					System.out.println("loading existing catalogue from DB "+catalogue.getSearchHistory().size());
					logger.info("loading existing catalogue from DB"+catalogue.getSearchHistory().size());
					HashSet<String> successfullQueries = catalogue.getSuccessfullQueries();
					for (Iterator iterator = successfullQueries.iterator(); iterator.hasNext();) {
						String querystring= (String) iterator.next();
						System.out.println("catalouged "+ querystring);
						query=model_db.query();
						
					}
					
					for (Iterator iterator = catalogue.getSearchHistory().iterator(); iterator.hasNext();) {
						ConceptSearchQueryResult resultSet= (ConceptSearchQueryResult) iterator.next();
						if(resultSet.getSearchResult()!=null){
						HashSet<IfdConcept> searchResult = resultSet.getSearchResult();
						for (IfdConcept ifdConcept : searchResult) {
							if (conceptGUIDS.contains(ifdConcept.getGuid().toString())==false){
								conceptGUIDS.add(ifdConcept.getGuid().toString());
								conceptcount++;
							List<IfdName> fullNames = ifdConcept.getFullNames();
							for (IfdName ifdName : fullNames) {
								namecount++;
						//		System.out.print(ifdName.getName());
							}
							}
						//	System.out.println("");
						}
						}
						
					}
			}
			else {
				logger.info("creating new catalogue from DB");
				catalogue = new DBSearchCatalogue();
				//model_db.store(catalogue);
				//model_db.commit();
			}
			System.out.println(conceptcount+" concepts with "+namecount+" names");
			model_db.close();
		}
	

}
